package com.knight.hadoop.day08.mr.wcdemo;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


/**
 * 相当于一个yarn集群的客户端
 * 需要再次封装我们的mr程序的相关运行参数和指定jar包
 * @author 
 *
 */
public class WordCountDriver {
	public static void main(String[] args) throws Exception {
		
		/*if(args==null || args.length==0){
			args = new String[]{"hdfs://hadoop4:9000/customWC/input","hdfs://hadoop4:9000/customWC/output"};
		}*/
		//配置本地运行模式时，hadoop的home路径
		//System.setProperty("hadoop.home.dir", "D:\\application\\hadoop-2.6.1");
		
		Configuration configuration = new Configuration();
		
		//windows运行配置,这也是默认值
		/*configuration.set("mapreduce.framework", "local");
		configuration.set("fs.defaultFS", "file:///");*/
		
		Job job = Job.getInstance(configuration);
		
		//指定本程序的jar包所在的本地路径
		job.setJarByClass(WordCountDriver.class);
		
		//设置mapper任务执行类
		job.setMapperClass(WordCountMapper.class);
		//设置reducer任务执行类
		job.setReducerClass(WordCountReducer.class);
		
		//指定mapper输出数据的kv类型
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);
		
		//指定最终输出数据的kv类型
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		
		//指定输入参数的目录
		FileInputFormat.setInputPaths(job, new Path(args[0]));
		//指定输出参数的目录
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		
		//将job配置的参数以及job所用的java类所在的jar包，提交到yarn去运行
		/*job.submit();*/
		//但是我们一般用这个，因为可以等待运行结果返回，查看运行流程
		boolean res = job.waitForCompletion(true);
		System.exit(res?0:1);
	}
}
